// pages/bindCard/bindCard.js
const httpUtils = require('../../utils/httpUtils.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: 'https://yxpmaterial.sse100.com/mall/',
    bankname: "请选择",
    banknameId:'',
    bankData: null,
    bank_code: '',
    bank_sub_name: '',
    bank_account_name: ''
  },
  getData: function () {
    let that = this
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    httpUtils.getbank({
      access_token: app.globalData.$TOKEN,
    }).then((res) => {
      that.setData({
        bankData: res.data
      })

    });

  },
  bindBank: function () {
    let that = this
    if (that.data.bank_account_name == "") {
    
      wx.showToast({
        title: "请输入开户姓名",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (that.data.bank_code == "") {
     
      wx.showToast({
        title: "请输入银行卡号",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (that.data.bankname == "请选择") {
     
      wx.showToast({
        title: "请选择银行",
        icon: 'none',
        duration: 2000
      })
      return
    }

    if (that.data.bank_sub_name == "") {
    
      wx.showToast({
        title: "请输入银行支行",
        icon: 'none',
        duration: 2000
      })
      return
    }

    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    httpUtils.bindbank({
      access_token: app.globalData.$TOKEN,
      bank_name: that.data.bankname,
      bank_code: that.data.bank_code,
      bank_sub_name: that.data.bank_sub_name,
      bank_account_name: that.data.bank_account_name
    }).then((res) => {
      wx.showToast({
        title: "绑定成功",
        icon: 'none',
        duration: 2000
      })
      wx.redirectTo({
        url: '/pages/fetchMoney/fetchMoney',
      })

    });

  },
  bindPickerChange: function (e) {
    let that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    that.setData({
      bankname: that.data.bankData[e.detail.value].bank_name,
      banknameId:that.data.bankData[e.detail.value].id,
    })
  },
  /**
   * 保存
   */
  goSave: function () {
    this.bindBank()
  },
  namechange: function (e) {
    let that = this;
    that.setData({
      bank_account_name: e.detail.value
    })

  },
  codechange: function (e) {
    let that = this;
    that.setData({
      bank_code: e.detail.value
    })

  },
  subchange: function (e) {
    let that = this;
    that.setData({
      bank_sub_name: e.detail.value
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})